﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.Commons;
using YT.DB;

namespace YT.XH.IgE
{
    public partial class frmIgEdetectHdr : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormulaType mFormulaType = new FormulaType();

        public frmIgEdetectHdr()
        {
            InitializeComponent();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            ShowMtn(0);
        }

        private void ShowMtn(long lngRecID)
        {
            frmIgEdetectInfo frmInfo = new frmIgEdetectInfo(FormStatus.AddNew);
            frmInfo.StartPosition = FormStartPosition.CenterScreen;
            Rectangle rect = System.Windows.Forms.SystemInformation.VirtualScreen;
            frmInfo.Width = rect.Width - 120;
            frmInfo.Height = rect.Height - 120;

            frmInfo.ShowData(lngRecID, mFormulaType);

            FillOrderByStatus();
        }

        private void frmIgEdetectHdr_Load(object sender, EventArgs e)
        {
            this.Text = SysVar.CurrentMenuInfo.strMenuName;
            this.Name = SysVar.CurrentMenuInfo.strMenuId;

            if (SysVar.CurrentMenuInfo.strMenuId == "600300010000000")
            {
                mFormulaType = FormulaType.TotalIgG4;
            }
            else if (SysVar.CurrentMenuInfo.strMenuId == "600300020000000")
            {
                mFormulaType = FormulaType.IgG4;
            }
            else if (SysVar.CurrentMenuInfo.strMenuId == "600300030000000")
            {
                mFormulaType = FormulaType.IgE;
            }

            FillOrderByStatus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void FillOrderByStatus()
        {
            string strStatusCond = "";
            string strSQL = "select * from IgEDetectHdr where FormulaType='" + mFormulaType + "' {0} Order by CheckDate Desc,DetectID Desc";
            if (btnAll.Checked)
            {
                strStatusCond = " and 1=1 ";
            }
            else if (btnUnPosted.Checked)
            {
                strStatusCond = " and Posted=0 ";
            }
            else if (btnPosted.Checked)
            {
                strStatusCond = " and Posted=1 ";
            }
            strSQL = string.Format(strSQL, strStatusCond);
            FillGrid(strSQL);
        }

        private void FillGrid(string strSQL)
        {
            if (strSQL == "") return;
            this.dgvDetail.Rows.Clear();   
            DataTable dtGrid = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drGrid in dtGrid.Rows)
            {
                this.dgvDetail.Rows.Add();
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColSeqNo.Name].Value = this.dgvDetail.Rows.Count;
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[colDetectID.Name].Value = drGrid["DetectID"];
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[colDate.Name].Value =BaseFunctions.FormatDate( CvtFunc.ToString (  drGrid["CheckDate"]), DateFormatOption.DateFmtYMD).ToString()  ;
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColRemark.Name].Value = CvtFunc.ToString(drGrid["Remark"]);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[colUser.Name].Value = drGrid["CheckName"];
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColStandardFormula.Name].Value = drGrid["StandardFormula"];
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[colParaR.Name].Value = drGrid["ParaR"];
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColRecID.Name].Value = drGrid["RecID"];
            }
        }

        private void frmIgEdetectHdr_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void frmIgEdetectHdr_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdbcon.CloseCon(); 
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
           DataFunctions.ExportDataGridViewToExcel(this.dgvDetail,colDel.Name);
        }

        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == colDetail.Index)
            {
                if (e.RowIndex < 0) return;
                if (this.dgvDetail.CurrentCell == null) return;
                long lngRecID = CvtFunc.ToLong(  this.dgvDetail.CurrentRow.Cells[ColRecID.Name].Value);
                ShowMtn(lngRecID);
            }
            else if (e.ColumnIndex == colDel.Index)
            {
                if (DeleteHdr(this.dgvDetail.Rows[e.RowIndex]))
                {
                    this.dgvDetail.Rows.Remove(this.dgvDetail.Rows[e.RowIndex]);
                    if (e.RowIndex - 1>0) this.dgvDetail.CurrentCell = this.dgvDetail.Rows[e.RowIndex - 1].Cells[ColSeqNo.Name]; 
                }
            }
        }

        private bool DeleteHdr(DataGridViewRow dgvRow)
        {
            
            string strSQL = "";

            string strRecID =CvtFunc.ToString ( dgvRow.Cells[ColRecID.Name].Value );

            if (CvtFunc.ToInt(strRecID) > 0)
            {
                if (CvtFunc.ToBoolean(mdbcon.GetValue("select posted from IgEDetectHdr where RecID='" + strRecID + "'")))
                {
                    MessageBox.Show("此检测报告已经审核，不能删除！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return false;
                }
                if (MessageBox.Show("确定要删除此检测?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                {
                    return false;
                }
            }

            strSQL = "Delete From IgEDetectHdr Where RecID='" + strRecID + "'";
            mdbcon.ExecuteSql(strSQL); 
            strSQL = "Delete From IgEDetectDetail Where HdrRecID='" + strRecID + "'";
            mdbcon.ExecuteSql(strSQL);
            strSQL = "Delete From IgEStandardFormula Where DetectID='"+ dgvRow.Cells[colDetectID.Name].Value  +"' ";
            mdbcon.ExecuteSql(strSQL);

            return true;
        }

        private void btnPosted_Click(object sender, EventArgs e)
        {
            btnPosted.Checked = true;
            btnUnPosted.Checked = false;
            btnAll.Checked = false;
            FillOrderByStatus();
        }

        private void btnAll_Click(object sender, EventArgs e)
        {
            btnPosted.Checked = false ;
            btnUnPosted.Checked = false ;
            btnAll.Checked = true ;
            FillOrderByStatus();
        }

        private void btnUnPosted_Click(object sender, EventArgs e)
        {
            btnPosted.Checked = false ;
            btnUnPosted.Checked = true  ;
            btnAll.Checked = false  ;
            FillOrderByStatus();
        }
    }
}
